<template>
  <div class="customer-form-page">
    <!-- 头部导航 -->
    <header class="header">
      <div class="container">
        <div class="logo">
          <router-link to="/" class="logo-link">
            <h1>岗位信息分享平台</h1>
          </router-link>
        </div>
        <nav class="nav">
          <router-link to="/" class="nav-item">首页</router-link>
          <router-link to="/login" v-if="!userStore.isLoggedIn" class="nav-item">登录</router-link>
          <router-link to="/user" v-if="userStore.isLoggedIn" class="nav-item">个人中心</router-link>
        </nav>
      </div>
    </header>

    <!-- 表单内容 -->
    <div class="form-content">
      <div class="container">
        <div class="form-layout">
          <!-- 左侧岗位信息 -->
          <div class="job-info-sidebar">
            <div class="job-card" v-if="job">
              <h3>岗位信息</h3>
              <div class="job-details">
                <h4>{{ job.title }}</h4>
                <p class="company">{{ job.company }}</p>
                <div class="job-meta">
                  <span class="location">{{ job.location }}</span>
                  <span class="salary">{{ job.salary }}</span>
                </div>
                <p class="description">{{ job.description }}</p>
              </div>
              
              <div class="share-info" v-if="shareUser">
                <h4>分享者信息</h4>
                <div class="share-user">
                  <div class="user-avatar">
                    <el-icon><User /></el-icon>
                  </div>
                  <div class="user-details">
                    <p class="user-name">{{ shareUser.name }}</p>
                    <p class="user-role">{{ shareUser.role === 'sales' ? '业务员' : '管理员' }}</p>
                  </div>
                </div>
                
                <div class="contact-info">
                  <div class="contact-item">
                    <el-icon><Phone /></el-icon>
                    <span>{{ shareUser.phone }}</span>
                  </div>
                  <div class="contact-item">
                    <el-icon><ChatDotRound /></el-icon>
                    <span>{{ shareUser.wechat }}</span>
                  </div>
                  <div class="contact-item">
                    <el-icon><Message /></el-icon>
                    <span>{{ shareUser.email }}</span>
                  </div>
                </div>
              </div>
            </div>
          </div>

          <!-- 右侧表单 -->
          <div class="form-main">
            <div class="form-card">
              <div class="form-header">
                <h2>客户登记表</h2>
                <p>请填写您的个人信息，我们会尽快与您联系</p>
              </div>
              
              <el-form
                :model="customerForm"
                :rules="customerRules"
                ref="customerFormRef"
                label-width="100px"
                class="customer-form"
              >
                <el-form-item label="姓名" prop="name">
                  <el-input v-model="customerForm.name" placeholder="请输入您的姓名" />
                </el-form-item>
                
                <el-form-item label="手机号" prop="phone">
                  <el-input v-model="customerForm.phone" placeholder="请输入您的手机号" />
                </el-form-item>
                
                <el-form-item label="微信号" prop="wechat">
                  <el-input v-model="customerForm.wechat" placeholder="请输入您的微信号" />
                </el-form-item>
                
                <el-form-item label="邮箱" prop="email">
                  <el-input v-model="customerForm.email" placeholder="请输入您的邮箱" />
                </el-form-item>
                
                <el-form-item label="工作经验" prop="experience">
                  <el-select v-model="customerForm.experience" placeholder="请选择工作经验" style="width: 100%">
                    <el-option label="应届毕业生" value="应届毕业生" />
                    <el-option label="1年以下" value="1年以下" />
                    <el-option label="1-3年" value="1-3年" />
                    <el-option label="3-5年" value="3-5年" />
                    <el-option label="5年以上" value="5年以上" />
                  </el-select>
                </el-form-item>
                
                <el-form-item label="学历" prop="education">
                  <el-select v-model="customerForm.education" placeholder="请选择学历" style="width: 100%">
                    <el-option label="高中及以下" value="高中及以下" />
                    <el-option label="大专" value="大专" />
                    <el-option label="本科" value="本科" />
                    <el-option label="硕士" value="硕士" />
                    <el-option label="博士" value="博士" />
                  </el-select>
                </el-form-item>
                
                <el-form-item label="期望薪资" prop="expectedSalary">
                  <el-input v-model="customerForm.expectedSalary" placeholder="请输入期望薪资" />
                </el-form-item>
                
                <el-form-item label="求职意向" prop="intention">
                  <el-input
                    v-model="customerForm.intention"
                    type="textarea"
                    :rows="3"
                    placeholder="请描述您的求职意向、技能特长等"
                  />
                </el-form-item>
                
                <el-form-item>
                  <el-button
                    type="primary"
                    size="large"
                    @click="submitForm"
                    :loading="loading"
                    block
                  >
                    提交申请
                  </el-button>
                </el-form-item>
              </el-form>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { useJobsStore } from '@/stores/jobs'
import { mockJobs, mockUsers } from '@/mock/data'
import { ElMessage } from 'element-plus'
import {  getPositionDetail, userGet, customersAdd } from "@/api/system"

const route = useRoute()
const router = useRouter()
const userStore = useUserStore()
const jobsStore = useJobsStore()

const customerFormRef = ref()
const loading = ref(false)
const job = ref(null)
const shareUser = ref(null)

const customerForm = ref({
  name: '',
  phone: '',
  wechat: '',
  email: '',
  experience: '',
  education: '',
  expectedSalary: '',
  intention: ''
})

const customerRules = {
  name: [
    { required: true, message: '请输入姓名', trigger: 'blur' }
  ],
  phone: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
  ],
  wechat: [
    { required: true, message: '请输入微信号', trigger: 'blur' }
  ],
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
  ],
  experience: [
    { required: true, message: '请选择工作经验', trigger: 'change' }
  ],
  education: [
    { required: true, message: '请选择学历', trigger: 'change' }
  ],
  expectedSalary: [
    { required: true, message: '请输入期望薪资', trigger: 'blur' }
  ],
  intention: [
    { required: true, message: '请填写求职意向', trigger: 'blur' }
  ]
}

const getJobDetail = async (jobId) => {
  const userInfo = await getPositionDetail(jobId)
  job.value = userInfo.data
}
const getShareDetail = async (jobId,shareUserId) => {
  const userInfo = await userGet(shareUserId)
  shareUser.value = userInfo.data
}

// 初始化数据
onMounted(() => {
  const jobId = parseInt(route.params.jobId)
//  const foundJob = mockJobs.find(j => j.id === jobId)

  if (jobId) {
    getJobDetail(jobId)

    const shareUserId = route.query.shareUserId ? parseInt(route.query.shareUserId) : null
    if(shareUserId){
      getShareDetail(jobId,shareUserId)
    }
  } else {
    ElMessage.error('岗位信息不存在')
    router.push('/')
  }
})

// 提交表单
const submitForm = async () => {
  try {
    await customerFormRef.value.validate()
    loading.value = true
    
    // 模拟提交
    const registration = {
      id: Date.now(),
      jobId: job.value.positionId,
      shareUserId: shareUser.value?.id || 1,
      customerName: customerForm.value.name,
      phone: customerForm.value.phone,
      wechat: customerForm.value.wechat,
      email: customerForm.value.email,
      experience: customerForm.value.experience,
      education: customerForm.value.education,
      expectedSalary: customerForm.value.expectedSalary,
      intention: customerForm.value.intention,
      registerTime: new Date().toLocaleString(),
      status: 'pending'
    }

    await customersAdd(registration)
//    jobsStore.addCustomerRegistration(registration)
    
    ElMessage.success('申请提交成功！我们会尽快与您联系。')
    
    // 重置表单
    customerForm.value = {
      name: '',
      phone: '',
      wechat: '',
      email: '',
      experience: '',
      education: '',
      expectedSalary: '',
      intention: ''
    }
    
    // 延迟跳转到首页
    setTimeout(() => {
      router.push('/')
    }, 2000)
    
  } catch (error) {
    ElMessage.error('请完善申请信息')
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.customer-form-page {
  min-height: 100vh;
  background-color: #f5f5f5;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

/* 头部样式 */
.header {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  padding: 20px 0;
  box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}

.header .container {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.logo-link {
  color: white;
  text-decoration: none;
}

.logo h1 {
  margin: 0;
  font-size: 24px;
  font-weight: bold;
}

.nav {
  display: flex;
  align-items: center;
  gap: 20px;
}

.nav-item {
  color: white;
  text-decoration: none;
  padding: 8px 16px;
  border-radius: 4px;
  transition: background-color 0.3s;
}

.nav-item:hover {
  background-color: rgba(255,255,255,0.2);
}

/* 表单内容 */
.form-content {
  padding: 40px 0;
}

.form-layout {
  display: grid;
  grid-template-columns: 1fr 2fr;
  gap: 30px;
}

/* 侧边栏 */
.job-info-sidebar {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.job-card {
  background: white;
  border-radius: 8px;
  padding: 20px;
  box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}

.job-card h3 {
  color: #333;
  margin: 0 0 15px 0;
  font-size: 18px;
}

.job-details h4 {
  color: #333;
  margin: 0 0 10px 0;
  font-size: 16px;
}

.company {
  color: #666;
  margin: 0 0 10px 0;
  font-weight: bold;
}

.job-meta {
  display: flex;
  gap: 15px;
  margin-bottom: 15px;
}

.location,
.salary {
  color: #666;
  font-size: 14px;
}

.salary {
  color: #e74c3c;
  font-weight: bold;
}

.description {
  color: #666;
  line-height: 1.6;
  margin: 0 0 20px 0;
}

.share-info h4 {
  color: #333;
  margin: 0 0 15px 0;
  font-size: 16px;
}

.share-user {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 15px;
}

.user-avatar {
  width: 40px;
  height: 40px;
  background: #667eea;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 16px;
}

.user-name {
  font-weight: bold;
  color: #333;
  margin: 0 0 5px 0;
}

.user-role {
  color: #666;
  margin: 0;
  font-size: 12px;
}

.contact-info {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.contact-item {
  display: flex;
  align-items: center;
  gap: 8px;
  color: #666;
  font-size: 14px;
}

/* 主表单 */
.form-main {
  display: flex;
  justify-content: center;
}

.form-card {
  background: white;
  border-radius: 8px;
  padding: 40px;
  box-shadow: 0 2px 10px rgba(0,0,0,0.1);
  width: 100%;
  max-width: 600px;
}

.form-header {
  text-align: center;
  margin-bottom: 30px;
}

.form-header h2 {
  color: #333;
  margin: 0 0 10px 0;
  font-size: 24px;
}

.form-header p {
  color: #666;
  margin: 0;
  font-size: 14px;
}

.customer-form {
  width: 100%;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .container {
    padding: 0 15px;
  }
  
  .header .container {
    flex-direction: column;
    gap: 15px;
  }
  
  .nav {
    flex-wrap: wrap;
    justify-content: center;
  }
  
  .form-layout {
    grid-template-columns: 1fr;
  }
  
  .form-card {
    padding: 30px 20px;
  }
  
  .job-meta {
    flex-direction: column;
    gap: 5px;
  }
}
</style> 